set more off
*version 14
local stick "C:\Users\Andy baker\OneDrive - University of Colorado at Boulder Office 365"
capture log close
log using "`stick'\My Documents\Research\Paper PID Experiment\USA\JOP IRT USA 2012 Results.log", text replace

use "`stick'\My Documents\Research\Paper PID Experiment\USA\ANES\anes_timesseries_2012_trimmed.dta", clear
svyset [pweight=weight_full]

*I. PARTY ID MEASURES
	*1: Classic measures
rename pid_x pid
mvdecode pid, mv(-2)
replace pid=pid-1
label drop _pid_x

recode pid (0/1=1) (2/4=0) (5/6=2) (.=.), gen(pidtraditional)
recode pid (0/2=1) (3=0) (4/6=2) (.=.), gen(pidmyth)

recode pid (0/4=0) (5/6=1) (.=.), gen(pidtraditionalrep)
recode pid (0/1=1) (2/6=0) (.=.), gen(pidtraditionaldem)

recode pid (0/3=0) (4/6=1) (.=.), gen(pidmythrep)
recode pid (0/2=1) (3/6=0) (.=.), gen(pidmythdem)

recode pid (0/3=0) (4=1) (5=2) (6=3) (.=.), gen(pidrep)
recode pid (0=3) (1=2) (2=1) (3/6=0) (.=.), gen(piddem)

svy: prop pid 


	*2: Are you close measure
*Generate "are you close" variable: binary
gen closerep=0 if cses_closepty==2
replace closerep=0 if cses_closepty==1 & cses_ptyclost==1
replace closerep=1 if cses_closepty==1 & cses_ptyclost==2 

svy: prop cses_closepty if cses_closepty>0

gen closedem=0 if cses_closepty==2
replace closedem=0 if cses_closepty==1 & cses_ptyclost==2
replace closedem=1 if cses_closepty==1 & cses_ptyclost==1 

*Generate "are you close" variable: ordinal
gen closereplong=0 if cses_closepty==2
	replace closereplong=0 if cses_closepty==1 & cses_ptyclost==1
replace closereplong=1 if cses_closepty==1 & cses_ptyclost==2 
replace closereplong=3 if closereplong==1 & cses_degclose==1
replace closereplong=2 if closereplong==1 & cses_degclose==2
replace closereplong=1 if closereplong==1 & cses_degclose==3


gen closedemlonglong=0 if cses_closepty==2
replace closedemlong=0 if cses_closepty==1 & cses_ptyclost==2
replace closedemlong=1 if cses_closepty==1 & cses_ptyclost==1 
replace closedemlong=3 if closedemlong==1 & cses_degclose==1
replace closedemlong=2 if closedemlong==1 & cses_degclose==2
replace closedemlong=1 if closedemlong==1 & cses_degclose==3

	*3: Party Registration
recode prevote_regpty_state (-1=.) (0=0) (1=0), gen(reg_dem)
replace reg_dem=1 if prevote_regpty==1

recode prevote_regpty_state (-1=.) (0=0) (1=0), gen(reg_rep)
replace reg_rep=1 if prevote_regpty==2


*II. OTHER MEASURES RELATED TO PARTY ID
	*1: Generate feeling thermometers
recode ft_dem (0/5=0) (6/15=1) (16/25=2) (26/35=3) (36/45=4) (46/55=5) (56/65=6) (66/75=7) (76/85=8) (86/95=9) (96/100=10) (else=.) 
recode ft_rep (0/5=0) (6/15=1) (16/25=2) (26/35=3) (36/45=4) (46/55=5) (56/65=6) (66/75=7) (76/85=8) (86/95=9) (96/100=10) (else=.) 
gen thermdemminusrep=(ft_dem-ft_rep)+10
gen thermrepminusdem=(ft_rep-ft_dem)+10

	*2: Generate vote
recode postvote_presvtwho (1=1) (2=0) (5=0) (-1=0) (else=.), gen(demvote)
recode postvote_presvtwho (2=1) (1=0) (5=0) (-1=0) (else=.), gen(repvote)

*III. Descriptives
summ pidtraditionalrep pidmythrep closerep ft_rep repvote  pidtraditionaldem pidmythdem closedem ft_dem demvote 

svy: mean pidtraditionalrep 
estat sd

svy: mean pidmythrep
estat sd

svy: mean closerep
estat sd

svy: mean ft_rep
estat sd

svy: mean repvote
estat sd

svy: mean pidtraditionaldem 
estat sd

svy: mean pidmythdem
estat sd

svy: mean closedem
estat sd

svy: mean ft_dem
estat sd

svy: mean demvote
estat sd

lambda pidtraditionaldem demvote
lambda pidmythdem demvote
lambda closedem demvote

lambda pidtraditionalrep repvote
lambda pidmythrep repvote
lambda closerep repvote

*V: Factor Analy sis check for unidimensionality
polychoricpca pidtraditionaldem closedem ft_dem demvote
polychoricpca pidmythdem closedem ft_dem demvote
polychoricpca pidtraditionalrep closerep ft_rep repvote
polychoricpca pidmythrep closerep ft_rep repvote

*VI. Criterion-Validity check
gen myth_close=.
replace myth_close=-2 if closedem==1 
replace myth_close=-1 if pidmythdem==1 & cses_closepty==2
replace myth_close=0 if pid==3 & cses_closepty==2
replace myth_close=1 if pidmythrep==1 & cses_closepty==2
replace myth_close=2 if closerep==1
replace myth_close=. if myth_close==2 & pidmythrep~=1 & pid~=3
replace myth_close=. if myth_close==-2 & pidmythdem~=1 & pid~=3

label define party 2 "Agreed Reps" 1 "Contested Reps" 0 "Agreed nonpart's" -1 "Contested Dems" -2 "Agreed Dems"
label values myth_close party

recode postvote_presvtwho (1=1) (2=0) (5=.) (-1=.) (else=.), gen(votebin)
recode ft_dpc (0/5=0) (6/15=1) (16/25=2) (26/35=3) (36/45=4) (46/55=5) (56/65=6) (66/75=7) (76/85=8) (86/95=9) (96/100=10) (else=.) 
recode ft_rpc (0/5=0) (6/15=1) (16/25=2) (26/35=3) (36/45=4) (46/55=5) (56/65=6) (66/75=7) (76/85=8) (86/95=9) (96/100=10) (else=.) 
gen obamaminusromney=(ft_dpc-ft_rpc)

recode presapp_job_x -9=. -8=. 1=4 2=3 4=2 5=1

local i=-2
while `i'<=2 {
svy: mean votebin if myth_close==`i'
local i=`i'+1
}
local i=-2
while `i'<=2 {
svy: mean obamaminusromney if myth_close==`i'
local i=`i'+1
}
local i=-2
while `i'<=2 {
svy: mean presapp_job_x if myth_close==`i'
local i=`i'+1
}



svy: mean votebin if myth_close~=.
estat sd

svy: mean obamaminusromney if myth_close~=.
estat sd

svy: mean presapp_job_x if myth_close~=.
estat sd

recode myth_close -1=1 1=1 0=2 .=. else=0, gen(fns)
separate votebin, by(fns)
separate presapp_job_x, by(fns)
separate obamaminusromney, by(fns)

	*bar graphs
graph bar (mean) votebin?, over(myth_close, label(angle(0) labsize(small) alternate tick labgap(0)))  graphregion(color(white)) plotregion(lcolor(black)) ///
blabel(bar, format(%9.2f) size(small)) bar(1, color(black)) bar(2, color(gs12)) bar(3, color(gs16) lcolor(black)) title("B1: Obama 2-party vote") ytitle("") ylab(, labsize(small))  legend(off) nofill
graph save "`stick'\My Documents\Research\Paper PID Experiment\USA\gr1.gph", replace

graph bar (mean) presapp_job_x?, over(myth_close, label(angle(0) labsize(small) alternate tick labgap(0))) graphregion(color(white)) plotregion(lcolor(black))  ///
blabel(bar, format(%9.2f) size(small)) bar(1, color(black)) bar(2, color(gs12)) bar(3, color(gs16) lcolor(black))   title("B2: Obama job approval") ytitle("") exclude0 yscale(range(1(.5)4)) ytick(1 2 3 4) ylab(1 2 3 4)  ylab(, labsize(small))  legend(off) nofill
graph save "`stick'\My Documents\Research\Paper PID Experiment\USA\gr3.gph", replace

graph bar (mean) obamaminusromney?, over(myth_close, label(angle(0) labsize(small) alternate tick labgap(0))) graphregion(color(white)) plotregion(lcolor(black))  ///
blabel(bar, format(%9.2f) size(small)) bar(1, color(black)) bar(2, color(gs12)) bar(3, color(gs16) lcolor(black))   title("B3: Obama - Romney feeling therms") ytitle("") ytick(-5 -2.5 0 2.5 5) ylab(-5 -2.5 0 2.5 5)  ylab(, labsize(small))  legend(off) nofill
graph save "`stick'\My Documents\Research\Paper PID Experiment\USA\gr4.gph", replace

graph combine ///
"`stick'\My Documents\Research\Paper PID Experiment\USA\gr1.gph" ///
"`stick'\My Documents\Research\Paper PID Experiment\USA\gr3.gph" ///
"`stick'\My Documents\Research\Paper PID Experiment\USA\gr4.gph", ///
row(2) col(2) graphregion(color(white))
graph export "`stick'\My Documents\Research\Paper PID Experiment\LaTeX\JOPUSACriterion.pdf", as(pdf) replace

*keep pidtraditionaldem closedem thermdemminusrep demvote weight_full
*save "`stick':\My Documents\Research\Paper PID Experiment\USA\ANES\anes_timesseries_2012_trimmed_to_stata.dta", replace

*V: Run IRT models
	*note that the nrm isn't quite right here because the proper comparison is (SD+WD)/(SR+WR+IR+I+ID) and (ID+SD+WD)/(SR+WR+IR+I).
	*nrm would do the following: (SD+WD)/(SR+WR+IR+I) and (ID)/(SR+WR+IR+I). Hence, need to run separate models
	
	*1: Reps Results
drop admin*
svy: irt grm pidtraditionalrep closerep ft_rep repvote, intpoints(30) difficult intmethod(mvaghermite)
predict probtraditionalrep, outcome(pidtraditionalrep #2) 
predict latenttradrep, latent

svy: irt grm pidmythrep closerep ft_rep repvote, intpoints(30) difficult intmethod(mvaghermite)
predict latentmythrep, latent
summ latentmythrep

test _b[pidmythrep:Theta]=_b[closerep:Theta]


irtgraph icc (ft_rep, lcolor(gs14) lwidth(vthin) lpattern(dash)) (pidmythrep closerep, lcolor(black) lwidth(medium)) (repvote, lcolor(black) lpattern(dash) lwidth(medium)), ///
bcc range(-1.31 1.96) legend(off) graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) b1title("{it: θ{subscript:Rep}}: Strength of Republican Partisanship") xtitle("") ytitle("{it:Pr}({it:x{subscript:j•Rep}}|{it: θ{subscript:Rep}})") title("") xtick(-2(.5)2) xlabel(-2(1)2) ///
addplot((scatter probtraditionalrep latenttradrep, msymbol(i) connect(l) sort lcolor(black) lwidth(medium)) ///
(scatteri .5 .22 "Think*({it:P{superscript:1}N{superscript:0}})", msymbol(i) mlabpos(0) mlabangle(83) mlabcolor(black) mlabsize(small)) ///
(scatteri .2 -.03 "Pres. vote", msymbol(i) mlabpos(0) mlabangle(46) mlabcolor(black) lpattern(dash) mlabsize(vsmall)) ///
(scatteri .25 .35 "Think({it:P{superscript:1}N{superscript:1}})", msymbol(i) mlabpos(0) mlabangle(68) mlabcolor(black) mlabsize(small)) ///
(scatteri .5 .86 "Close({it:P{superscript:0}N{superscript:1}})", msymbol(i) mlabpos(0) mlabangle(77) mlabcolor(black) mlabsize(small)))
graph export "`stick'\My Documents\Research\Paper PID Experiment\LaTeX\JOPRepsIRT.pdf", as(pdf) replace 

	*2: Dems Results
svy: irt grm pidtraditionaldem closedem ft_dem demvote, intpoints(30) difficult intmethod(mvaghermite)
predict probtraditionaldem, outcome(pidtraditionaldem #2)
predict latenttraddem, latent

svy: irt grm pidmythdem closedem ft_dem demvote, intpoints(30) difficult intmethod(mvaghermite) 
predict latentmythdem, latent
summ latentmythdem

test _b[pidmythdem:Theta]=_b[closedem:Theta]

irtgraph icc (ft_dem, lcolor(gs14) lwidth(vthin) lpattern(dash)) (pidmythdem closedem, lcolor(black) lwidth(medium)) (demvote, lcolor(black) lpattern(dash) lwidth(medium)), ///
bcc range(-1.5 1.74) legend(off) graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) b1title("{it: θ{subscript:Dem}}: Strength of Democratic Partisanship") xtitle("") ytitle("{it:Pr}({it:x{subscript:j•Dem}}|{it: θ{subscript:Dem}})") title("") xtick(-2(.5)2) xlabel(-2(1)2) ///
addplot((scatter probtraditionaldem latenttraddem, msymbol(i) connect(l) sort lcolor(black) lwidth(medium)) ///
(scatteri .5 .05 "Think*({it:P{superscript:1}N{superscript:0}})", msymbol(i) mlabpos(0) mlabangle(85) mlabcolor(black) mlabsize(small)) ///
(scatteri .22 -.16 "Pres. vote", msymbol(i) mlabpos(0) mlabangle(45) mlabcolor(black) lpattern(dash) mlabsize(vsmall)) ///
(scatteri .6 .49 "Think({it:P{superscript:1}N{superscript:1}})", msymbol(i) mlabpos(0) mlabangle(71) mlabcolor(black) mlabsize(small)) ///
(scatteri .5 .73 "Close({it:P{superscript:0}N{superscript:1}})", msymbol(i) mlabpos(0) mlabangle(70) mlabcolor(black) mlabsize(small)))  
graph export "`stick'\My Documents\Research\Paper PID Experiment\LaTeX\JOPDemsIRT.pdf", as(pdf) replace

*Item-rest validation
svy: irt grm closerep ft_rep repvote, intpoints(30) difficult intmethod(mvaghermite)
predict latentrep_rest, latent
polychoric latentrep_rest pidtraditionalrep pidmythrep, pw verbose

svy: irt grm closedem ft_dem demvote, intpoints(30) difficult intmethod(mvaghermite)
predict latentdem_rest, latent
polychoric latentdem_rest pidtraditionaldem pidmythdem, pw verbose

irt grm pidrep ft_rep repvote [pweight=weight_full], intpoints(30) difficult intmethod(mvaghermite)
predict latentrep_2, latent ebmodes
polychoric latentrep_2 closerep, pw verbose

irt grm piddem ft_dem demvote [pweight=weight_full], intpoints(30) difficult intmethod(mvaghermite)
predict latentdem_2, latent ebmodes
polychoric latentdem_2 closedem, pw verbose

*Item-test validation
irt grm pidrep closerep ft_rep repvote [pweight=weight_full], intpoints(30) difficult intmethod(mvaghermite)
predict latentrep_test, latent ebmodes
polychoric latentrep_test pidtraditionalrep pidmythrep closerep, pw verbose

irt grm piddem closedem ft_dem demvote [pweight=weight_full], intpoints(30) difficult intmethod(mvaghermite)
predict latentdem_test, latent ebmodes
polychoric latentdem_test pidtraditionaldem pidmythdem closedem, pw verbose


log close
